from django.db import models
from config.choices import ISLINK_CHOICES, TYPE_CHOICES, METHOD_CHOICES, DATASCOPE_CHOICES
from utils.models import CoreModel


# Create your models here.

class Role(CoreModel):
    name = models.CharField(max_length=50, verbose_name="角色名称", help_text="角色名称")
    code = models.CharField(max_length=50, verbose_name="角色编码", help_text="角色编码")
    data_range = models.IntegerField(choices=DATASCOPE_CHOICES, default=1,
                                     verbose_name="数据权限范围", help_text="数据权限范围")

    class Meta:
        db_table = 'system_role'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class RoleMenu(CoreModel):
    role = models.ForeignKey(to="Role", on_delete=models.CASCADE, verbose_name="关联角色", help_text="关联角色")
    menu = models.ForeignKey(to="Menu", on_delete=models.CASCADE, verbose_name="关联菜单", help_text="关联菜单")

    class Meta:
        db_table = 'system_role_menu'
        verbose_name = '角色菜单关联表'
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class RoleMenuItem(CoreModel):
    role = models.ForeignKey(to="Role", on_delete=models.CASCADE, verbose_name="关联角色", help_text="关联角色")
    menu_button = models.ForeignKey(to="MenuButton", on_delete=models.CASCADE, verbose_name='关联菜单的接口按钮',
                                    help_text='关联菜单的接口按钮')

    class Meta:
        db_table = 'system_role_menu_button'
        verbose_name = '角色菜单权限关联表'
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class Menu(CoreModel):
    parent = models.ForeignKey(to='Menu', on_delete=models.CASCADE, verbose_name="上级菜单", null=True, blank=True,
                               db_constraint=False, help_text="上级菜单")
    icon = models.CharField(max_length=64, default='ant-design:book-outlined', verbose_name="菜单图标",
                            help_text="菜单图标")
    title = models.CharField(max_length=64, verbose_name="菜单名称", help_text="菜单名称")
    permission = models.CharField(max_length=64, null=True, blank=True, verbose_name="权限标识", help_text="权限标识")
    is_ext = models.BooleanField(choices=ISLINK_CHOICES, default=False, verbose_name="是否外链", help_text="是否外链")

    type = models.BooleanField(choices=TYPE_CHOICES, verbose_name="是否目录", help_text="是否目录")
    path = models.CharField(max_length=128, verbose_name="路由地址", null=True, blank=True, help_text="路由地址")
    redirect = models.CharField(max_length=128, verbose_name="重定向地址", null=True, blank=True,
                                help_text="重定向地址")
    component = models.CharField(max_length=128, verbose_name="组件地址", null=True, blank=True, help_text="组件地址")
    name = models.CharField(max_length=50, verbose_name="组件名称", null=True, blank=True, help_text="组件名称")
    status = models.BooleanField(default=True, blank=True, verbose_name="菜单状态", help_text="菜单状态")
    keepalive = models.BooleanField(default=False, blank=True, verbose_name="是否页面缓存", help_text="是否页面缓存")
    hide_menu = models.BooleanField(default=False, blank=True, verbose_name="侧边栏中是否隐藏",
                                    help_text="侧边栏中是否隐藏")

    class Meta:
        db_table = "system_menu"
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name
        ordering = ('create_time',)


class MenuButton(CoreModel):
    title = models.CharField(max_length=64, default="", null=True, blank=True, verbose_name="名称", help_text="名称")
    type = models.IntegerField(default=3, verbose_name="类型", help_text="类型")
    name = models.CharField(max_length=64, verbose_name="名称", help_text="名称")
    code = models.CharField(max_length=64, verbose_name="权限值", help_text="权限值")
    api = models.CharField(max_length=200, verbose_name="接口地址", help_text="接口地址")
    method = models.IntegerField(choices=METHOD_CHOICES, default=0, verbose_name="接口请求方法", null=True, blank=True,
                                 help_text="接口请求方法")
    menu = models.ForeignKey(to="Menu", db_constraint=False, related_name="menuPermission",
                             on_delete=models.CASCADE, verbose_name="关联菜单", help_text='关联菜单')

    class Meta:
        db_table = "system_menu_button"
        verbose_name = '菜单权限表'
        verbose_name_plural = verbose_name
        ordering = ('sort',)
